package com.xgimi.open.skill.datamanager.biz.utils;

import com.xgimi.open.skill.datamanager.biz.constant.NluConstants;
import org.springframework.stereotype.Service;

/**
 * @author coderzpw.zhang
 * @version 1.0
 * @description 描述
 * @date 2023/2/15 16:10
 */
@Service
public class RedisKeyBuilder {
    /**
     * <b>描述：</b> 系统码, 用于标识在Redis中的Key属于的系统
     */
    public static final String SYSTEM_CODE = "nlu";

    /**
     * <b>描述：</b> Redis Key各个部分的分隔器
     */
    public static final String KEY_SEPERATOR = "|";

    //  区别线上(prod)和沙箱(sandbox)环境 训练文件存储 模型文件缓存区分 (与open-skill-nlu服务的xgimi.skill.train.env.group配置对齐)
    public static final String SKILL_TRAIN_ENV_GROUP = "test.online";

    private RedisKeyBuilder() {
    }

    public static String buildNluTrainLockKey(String skillAppId) {
        return buildKey(NluConstants.NLU_TRAIN_LOCK_FLAG, skillAppId);
    }
    public static String buildSkillUpdateLockKey(String businessId) {
        return buildKey(businessId, NluConstants.NLU_TRAIN_LOCK_FLAG, SKILL_TRAIN_ENV_GROUP);
    }


    public static String buildNluSkillPublishKey(String skillId) {
        return buildKey(NluConstants.NLU_SKILL_PUBLISH_LOCK_FLAG, skillId);
    }

    public static String buildKey(String... elements) {
        if (elements == null || elements.length == 0) {
            throw new IllegalArgumentException("构建Key的参数不能为空数组");
        }

        StringBuilder builder = new StringBuilder(SYSTEM_CODE);
        for (String elem : elements) {
            builder.append(KEY_SEPERATOR).append(elem);
        }
        return builder.toString();
    }
}
